<?php
namespace app\shop\model;
use app\BaseModel;
use think\facade\Db;



class BargainRecordList extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_shop_bargain_record_list';


    /**
     * @author chenniang
     * @DataTime: 2021-09-24 13:54
     * @功能说明:
     */
    public function getPriceAttr($value,$data){

        if(isset($value)){

            if(!empty($value)){

                return $value;
            }

            if(!empty($data['record_id'])){

                $record_model = new BargainRecord();

                $atv = $record_model->dataInfo(['id'=>$data['record_id']]);
                //需要砍掉的金额
                $y_price = ($atv['init_price'] - $atv['low_price'])>0?$atv['init_price'] - $atv['low_price']:0;

                $have_price = $this->where(['record_id'=>$data['record_id']])->where('price','<>',0)->sum('price');

                $y_price = $y_price - $have_price;
                //空的有几人
                $empty_num = $this->where(['record_id'=>$data['record_id']])->where('price','=',0)->count();

                $y_num = $atv['user_num']-$atv['have_num'];
                //已经完了
                if($y_num<=0){

                    $price = round($y_price/$empty_num,2);

                }else{

                    $y_price = $y_price-$y_num*0.01;

                    $price = round($y_price/$empty_num,2);


                }

                return $price;

            }

        }


    }


    /**
     * @author chenniang
     * @DataTime: 2021-09-24 14:20
     * @功能说明:已经砍掉多少钱
     */
    public function havePrice($dis){

        $list = $this->where($dis)->select()->toArray();

        $price = 0;

        if(!empty($list)){

            $price = array_sum(array_column($list,'price'));
        }

        return round($price,2);

    }

    /**
     * @author chenniang
     * @DataTime: 2020-12-09 14:17
     * @功能说明:列表
     */
    public function dataList($dis,$page=10){

        $data = $this->alias('a')
                ->join('longbing_card_user b','a.user_id = b.id','left')
                ->where($dis)
                ->field('a.create_time,b.nickName,b.avatarUrl,a.price,a.record_id')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-09 14:18
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

//        $data['update_time'] = time();

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-19 10:36
     * @功能说明:编辑
     */
   public function dataUpdate($dis,$data){

       $data['update_time'] = time();

       $res = $this->where($dis)->update($data);
       
       return $res;

   }

    /**
     * @author chenniang
     * @DataTime: 2021-01-19 10:46
     * @功能说明:详情
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];


    }












}